package core
{
	import alternativa.engine3d.controllers.CameraController;
	import alternativa.engine3d.controllers.FlyController;
	import alternativa.engine3d.controllers.WalkController;
	import alternativa.engine3d.core.Camera3D;
	import alternativa.engine3d.core.Object3D;
	import alternativa.engine3d.core.Scene3D;
	import alternativa.engine3d.display.View;
	import alternativa.engine3d.primitives.Box;
	import alternativa.utils.FPS;
	
	import flash.events.Event;
	import flash.utils.setTimeout;
	
	import mx.core.UIComponent;

	public class GameView extends UIComponent
	{


		private var _view:View;
		private var _cam:Camera3D;
		private var _scene:Scene3D;
		
		private var _skybox:SkyBox;
		private var _player:Player;
		private var _box:Box;
		
		private var cameraController:CameraController;
		private var _fly:FlyController;
		private var _walk:WalkController;
		
		public var controllers:Array = [];
		
		public function GameView()
		{
			super();
			init();
		}
		
		private function init():void
		{
			
			
			// Creating scene
			_scene = new Scene3D();
			_scene.root = new Object3D();
			_cam = new Camera3D();
			_cam.x = 100;
			_cam.y = -150;
			_cam.z = 100;
			//_scene.root.addChild(_cam);
			_view = new View();
			addChild(_view);
			_view.camera = _cam;
			
			_skybox = Factory.createSkybox();
			_scene.root.addChild(_skybox);
			
			_player = Factory.createPlayer();
			_scene.root.addChild(_player);
			_scene.root.addChild(_cam);
			_cam.z = -200;
			
			//_box = Factory.createCube();
			//_scene.root.addChild(_box);
			

			
			this.addEventListener(Event.RESIZE, resize);
			resize(null);
			
			setTimeout(FPSinit, 500);
		}
		
		private function FPSinit():void
		{
			_walk = new WalkController(systemManager.stage);
			_walk.object = _cam;
			_walk.lookAt(_player.coords);
			_walk.setDefaultBindings();
			_walk.unbindAll()
			
			/* _fly = new FlyController(systemManager.stage);
			_fly.object = _cam;
			_fly.setDefaultBindings(); */
			FPS.init(systemManager.stage);
			this.addEventListener(Event.ENTER_FRAME, render);
		
		}
		
		private function render(e:Event):void
		{
			//_player.rotationX++;
			//_box.rotationX += 0.01;
			//_box.rotationZ -= 0.001;
			_walk.processInput();

			//_fly.processInput();
			_scene.calculate();
		}
		
		protected function resize(e:Event):void
		{
			if(_view)
			{
				_view.width = width;
				_view.height = height;
			}
		}
		
	}
}